package com.nuc.bigdata.app.service.impl;

import com.nuc.bigdata.app.service.CommodityService;
import com.nuc.bigdata.domain.entity.User;
import com.nuc.bigdata.domain.vo.CommentsVO;
import com.nuc.bigdata.domain.vo.CommodityDetailVO;
import com.nuc.bigdata.domain.vo.CommodityPicAndDescVO;
import com.nuc.bigdata.domain.vo.CommodityVO;
import com.nuc.bigdata.feign.CommodityServiceFeign;
import com.nuc.bigdata.feign.NewsCommentsServiceFeign;
import com.nuc.bigdata.feign.PictureServiceFeign;
import com.nuc.bigdata.feign.UserServiceFeign;
import com.nuc.bigdata.feign.dto.NewsCommentsDto;
import com.nuc.bigdata.feign.entity.CommentsFeign;
import com.nuc.bigdata.feign.entity.CommodityCommentsFeign;
import com.nuc.bigdata.feign.entity.CommodityDetailFeign;
import com.nuc.bigdata.feign.entity.CommodityFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/28
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    private final CommodityServiceFeign commodityService;
    private final PictureServiceFeign pictureService;
    private final UserServiceFeign userService;
    private final NewsCommentsServiceFeign commentsService;

    @Autowired
    CommodityServiceImpl(CommodityServiceFeign commodityService,
                         PictureServiceFeign pictureService,
                         UserServiceFeign userService,
                         NewsCommentsServiceFeign commentsService) {
        this.commodityService =commodityService;
        this.pictureService = pictureService;
        this.userService = userService;
        this.commentsService = commentsService;
    }


    /**
     * 列出所有的简略商品信息
     * @param pageSize 页码
     * @return 商品VO的ArrayList
     */
    @Override
    public ArrayList<CommodityVO> listAll(Long pageSize) {
        ArrayList<CommodityFeign> commodityArrayList = commodityService.listAllCommodity(pageSize);
        ArrayList<CommodityVO> voArrayList = new ArrayList<>();
        for (CommodityFeign feign:commodityArrayList) {
            CommodityVO vo = transformFeign(feign);
            voArrayList.add(vo);
        }
        return voArrayList;
    }

    /**
     * 根据商品唯一标识获取商品详情VO
     *
     * @param commodityId 商品唯一标识
     * @return 商品详情VO
     */
    @Override
    public CommodityDetailVO queryDetailById(String commodityId) {
        CommodityDetailFeign detailFeign = commodityService.queryDetailByCommodityId(commodityId);
        return transformDetailFeign(detailFeign);
    }

    /**
     * 根据商品唯一标识查询商品图文详情
     *
     * @param commodityId 商品唯一标识
     * @return 图文详情VO
     */
    @Override
    public CommodityPicAndDescVO queryPicAndDescById(String commodityId) {
        CommodityDetailFeign detailFeign = commodityService.queryDetailByCommodityId(commodityId);
        CommodityDetailVO commodityDetailVO = transformDetailFeign(detailFeign);
        CommodityPicAndDescVO commodityPicAndDescVO = new CommodityPicAndDescVO();
        commodityPicAndDescVO.setDescription(commodityDetailVO.getDescription());
        commodityPicAndDescVO.setPictureDetailsUrl(commodityDetailVO.getPictureDetailsUrl());
        commodityPicAndDescVO.setAbstractMsg(commodityDetailVO.getAbstractMsg());
        return commodityPicAndDescVO;
    }

    /**
     * 根据商品唯一标识获取评论信息
     *
     * @param commodityId 商品唯一标识
     * @param pageSize 页码
     * @return 包含评论VO的ArrayList
     */
    @Override
    public ArrayList<CommentsVO> listByCommodityId(String commodityId,Long pageSize) {
        ArrayList<CommodityCommentsFeign> commentsFeigns = commentsService.queryCommodityCommentsFeign(commodityId, pageSize);
        ArrayList<CommentsVO> commentsVOS = new ArrayList<>();
        commentsFeigns.forEach(item -> commentsVOS.add(transformCommentsFegin(item)));
        return commentsVOS;
    }

    private CommentsVO transformCommentsFegin(CommodityCommentsFeign feign) {
        CommentsVO vo = new CommentsVO();
        vo.setComment(feign.getComment());
        vo.setCommentId(feign.getCommodityCommentId());
        vo.setCommentLike(feign.getCommentLike());
        vo.setCreateTime(feign.getCreateTime());
        vo.setNewsId(feign.getCommodityId());
        User user = userService.queryByUserId(feign.getCreatedBy());
        vo.setHeadPic(user.getHeadPicture());
        vo.setUserName(user.getLoginName());
        return vo;
    }

    /**
     * 创建评论
     *
     * @param commodityId 商品唯一标识
     * @param commentsDto 评论dto
     */
    @Override
    public void createComment(String commodityId, NewsCommentsDto commentsDto) {
        commentsService.createCommodityComments(commodityId, commentsDto);
    }

    /**
     * 点赞评论
     *
     * @param commodityCommentId 商品评论唯一标识
     * @param userId             用户唯一标识
     */
    @Override
    public void toCommentLike(String commodityCommentId, String userId) {
        commentsService.toCommodityCommentLike(commodityCommentId, userId);
    }

    /**
     * 获取缩略图url
     * @param pictureId 图片地址
     * @return url字符串
     */
    private String queryPictureUrl(String pictureId) {
        return pictureService.getThumbnailUrl(pictureId);
    }

    /**
     * 获取图片url
     * @param pictureId 图片唯一标识
     * @return 图片url字符串
     */
    private String queryMaxPictureUrl(String pictureId) {
        return pictureService.getPictureUrl(pictureId);
    }

    /**
     * 将商品feign转化为商品vo
     * @param feign 商品服务接口返回的商品feign
     * @return 商品VO
     */
    private CommodityVO transformFeign(CommodityFeign feign) {
        CommodityVO vo = new CommodityVO();
        vo.setAbstractMsg(feign.getAbstractMsg());
        vo.setAddTime(feign.getAddTime());
        vo.setClick(feign.getClick());
        vo.setCommodityId(feign.getCommodityId());
        vo.setCommodityUom(feign.getCommodityUom());
        String url = queryPictureUrl(feign.getHomePic());
        vo.setHomePicThumbUrl(url);
        vo.setSellPrice(feign.getSellPrice());
        vo.setStickerPrice(feign.getStickerPrice());
        vo.setStock(feign.getStock());
        vo.setTitle(feign.getTitle());
        return vo;
    }

    /**
     * 将商品feign转化为商品VO，主要转化图片url
     * @param feign 商品feign
     * @return 商品VO
     */
    private CommodityDetailVO transformDetailFeign(CommodityDetailFeign feign) {
        CommodityDetailVO vo = new CommodityDetailVO();
        vo.setAbstractMsg(feign.getAbstractMsg());
        vo.setAddTime(feign.getAddTime());
        vo.setCommodityId(feign.getCommodityId());
        vo.setCommodityUom(feign.getCommodityUom());
        vo.setDescription(feign.getDescription());
        // 获取详情图片url
        ArrayList<String> pictureDetails = feign.getPictureDetails();
        ArrayList<String> pictureDetailsUrl = new ArrayList<>();
        pictureDetails.forEach(item -> pictureDetailsUrl.add(queryMaxPictureUrl(item)));
        vo.setPictureDetailsUrl(pictureDetailsUrl);
        vo.setSellPrice(feign.getSellPrice());
        vo.setStickerPrice(feign.getStickerPrice());
        vo.setStock(feign.getStock());
        // 获取商品轮播图url
        ArrayList<String> viewPagers = feign.getViewPagers();
        ArrayList<String> viewPagersUrl = new ArrayList<>();
        viewPagers.forEach(item -> viewPagersUrl.add(queryMaxPictureUrl(item)));
        vo.setViewPagersUrl(viewPagersUrl);
        return vo;
    }
}
